package com.rk.user.api;
import com.rk.cmn.client.DictFeignClient;
import com.rk.common.result.Result;
import com.rk.common.util.AuthContextHolder;
import com.rk.hosp.enums.DictEnum;
import com.rk.hosp.model.user.UserInfo;
import com.rk.hosp.vo.user.LoginVo;
import com.rk.hosp.vo.user.UserAuthVo;
import com.rk.user.service.UserInfoService;
import com.rk.user.utils.IpUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author :Rk.
 * @date : 2022/11/21
 */
@RestController
@RequestMapping("/api/user")
public class UserInfoApiController {

    @Autowired
    private UserInfoService userInfoService;


    @Autowired
    private DictFeignClient dictFeignClient;


    @ApiOperation(value = "会员登录")
    @PostMapping("/login")
    public Result login(@RequestBody LoginVo loginVo, HttpServletRequest request) {
        loginVo.setIp(IpUtil.getIpAddr(request));
        Map<String, Object> info = userInfoService.login(loginVo);
        return Result.ok(info);
    }

    @ApiOperation(value = "用户认证")
    @PostMapping("/auth/userAuth")
    public Result userAuth(@RequestBody UserAuthVo userAuthVo,HttpServletRequest request){
        try {
            userInfoService.userAuth(AuthContextHolder.getUserId(request),userAuthVo);
        }catch (Exception e){
            return Result.fail("认证失败!");
        }
        return Result.ok("认证成功!");
    }


    @ApiOperation(value = "获取用户信息")
    @GetMapping("/auth/getUserInfo")
    public Result getUserInfo(HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);
        UserInfo user = userInfoService.getById(userId);
        String name = dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), user.getCertificatesType());
        Map<String,Object> map=new HashMap<>();
        map.put("certificatesTypeString",name);
        user.setParam(map);
        return Result.ok(user);
    }



}
